package com.housekeeping.housekeeping.order.wxPay.utils;

import com.github.wxpay.sdk.WXPay;
import com.housekeeping.housekeeping.order.utils.ResponseEntity;
import com.housekeeping.housekeeping.order.wxPay.config.PayConfiguration;
import com.housekeeping.housekeeping.order.wxPay.service.OrderPayServ;
import com.housekeeping.housekeeping.order.wxPay.service.impl.OrderPayServImpl;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class PayHelper {
    private WXPay wxPay;
    private OrderPayServ orderPayServ = new OrderPayServImpl();

    public PayHelper() {
    }

    public PayHelper(PayConfiguration payConfiguration) {
        // 真实开发时
        wxPay = new WXPay(payConfiguration);
//         测试时
//         wxPay = new WXPay(payConfiguration, WXPayConstants.SignType.MD5, true);
    }

    //借助WXPay接口生成支付链接
    public String createPayUrl(Long payId) {
        String key = "ly.pay.url." + payId;

        //支付信息设置
        try {
            Map<String, String> payData = new HashMap<>();
            // 商品描述
            payData.put("body", "微信支付");
            // 订单号
            payData.put("out_trade_no", payId.toString());
            //货币
            payData.put("fee_type", "CNY");
            //金额，单位是分
            payData.put("total_fee", "1");
            //调用微信支付的终端IP
            payData.put("spbill_create_ip", "127.0.0.1");
            //回调地址，付款成功后的接口
            payData.put("notify_url", "http://test.housekeeping.com/wxpay/notify");
            // 交易类型为扫码支付
            payData.put("trade_type", "NATIVE");
            //商品id,使用假数据
            payData.put("product_id", "1234567");

            //统一化订单
            Map<String, String> result = this.wxPay.unifiedOrder(payData);

            if ("SUCCESS".equals(result.get("return_code"))) {
                String payUrl = result.get("code_url");
                // 将付款地址缓存，时间为10分钟
                System.out.println("支付链接"+payUrl);
                return payUrl;
            } else {
                System.out.println("调用支付接口失败:"+result.get("return_msg"));
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 查询相应订单的状态
     */
    public ResponseEntity<Integer> queryOrderState(Long padId) {
        HashMap<String, String> payData = new HashMap<>();
        //指定订单号
        payData.put("out_trade_no", padId.toString());
        ResponseEntity<Integer> entity = null;
        try {
            Map<String, String> result = this.wxPay.orderQuery(payData);
            String trade_state = result.get("trade_state");
            if (result == null) {
                //未查询到结果，认为是未付款
                entity = ResponseEntity.error(WXPayCode.PAY_FAIL, PaySate.PAY_FAIL.getState());

            } else if ("SUCCESS".equals(trade_state)) {
                //付款成功
                entity = ResponseEntity.success(PaySate.PAY_SUCCESS.getState());
                //修改数据库的订单状态
                int count = orderPayServ.updatePayState(padId);
                if (count != 0) {
                    System.out.println("订单已支付");
                } else {
                    System.out.println("订单支付状态修改失败！");
                }
            } else if (StringUtils.equals("USERPAYING", trade_state) || StringUtils.equals("NOTPAY", trade_state)) {
                //用户正在付款或未付款
                entity = ResponseEntity.error(WXPayCode.PAY_FAIL, PaySate.PAY_FAIL.getState());
            } else {
                //其他状态认为是付款失败
                entity = ResponseEntity.error(WXPayCode.PAY_FAIL, PaySate.PAY_FAIL.getState());
            }

        } catch (Exception e) {
            //付款失败
            entity = ResponseEntity.error(WXPayCode.PAY_FAIL, PaySate.PAY_FAIL.getState());
        }

        return entity;
    }
}
